//
//  WishListViewController.m
//  iFoundIt
//
//  Created by Navid Shariat on 4/05/10.
//  Copyright 2010 QUT. All rights reserved.
//

#import "WishListViewController.h"
#import "WishedItem.h"
#import "WishItemViewController.h"

@implementation WishListViewController
@synthesize wishList;
@synthesize refreshButton;
@synthesize editButton;

const NSString * EDITTABLE_EDIT=@"Edit";
const NSString * EDITTABLE_DONE=@"Done";

- (void)dealloc {
	[wishList release];
	[refreshButton release];
	[editButton release];
	[EDITTABLE_DONE release];
	[EDITTABLE_EDIT release];	
	[wishlistManager release];
    [super dealloc];
}

- (void)viewDidLoad {
//	self.tableView.editing=YES;
	[self setTitle:@"Wish list"];
	NSMutableArray * newArray=[self retrieveWishList];
	[self addRefreshButton];
	[self addEditButton];
	[self setWishList:newArray];
	[newArray release];
}

- (WishlistPersistentManager*) wishlistManager{
	if (!wishlistManager)
		wishlistManager=[[WishlistPersistentManager alloc] init];
	return wishlistManager;
}

#pragma mark actions
- (IBAction)refreshButtonClicked{
	[self refreshTable];	
}

- (IBAction)editButtonClicked{
	if ([editButton title]==EDITTABLE_DONE){
		self.tableView.editing=NO;
		[editButton setTitle:EDITTABLE_EDIT];
	}
	else{
		self.tableView.editing=YES;
		[editButton setTitle:EDITTABLE_DONE];	
	}
}


#pragma mark methods
- (void) refreshTable{
	wishList=nil;
	NSMutableArray * newArray=[self retrieveWishList];
	[self setWishList:newArray];
	[newArray release];
	[self.tableView reloadData];
	
}

-  (NSMutableArray*)  retrieveWishList{
	return [[self wishlistManager] getWishlist];		
	}

- (void) deleteWishedItem: (WishedItem*) wishedItem{
	[[self wishlistManager] deleteItem:wishedItem];
	[self refreshTable];	
}

-  (void) navigateToDeailsView: (WishedItem  *) wishedItem{
	WishItemViewController *wishItemViewController= [[WishItemViewController alloc] initWithNibName:@"WishItemViewController" bundle:nil];
	[wishItemViewController setWishedItem:wishedItem];
	[[self navigationController]pushViewController:wishItemViewController animated:YES];
	[wishItemViewController release];				
}			


-(void) addRefreshButton{
	refreshButton = [[UIBarButtonItem alloc] init];
	[refreshButton setTitle:@"Refresh"];
	self.navigationItem.leftBarButtonItem = refreshButton;
	self.navigationItem.leftBarButtonItem.enabled = YES;
	self.navigationItem.leftBarButtonItem.target=self;	
	self.navigationItem.leftBarButtonItem.action=@selector(refreshButtonClicked);		
}

-(void) addEditButton{
	editButton = [[UIBarButtonItem alloc] init];
	[editButton setTitle:EDITTABLE_EDIT];
	self.navigationItem.rightBarButtonItem = editButton;
	self.navigationItem.rightBarButtonItem.enabled = YES;
	self.navigationItem.rightBarButtonItem.target=self;	
	self.navigationItem.rightBarButtonItem.action=@selector(editButtonClicked);		
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[self wishList]count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
		[cell setAccessoryType: UITableViewCellAccessoryDisclosureIndicator];
//		[cell setAccessoryType: UITableViewCellAccessoryDetailDisclosureButton];
    }
    
    // Set up the cell...
	NSUInteger row= [indexPath row];
	//	NSUInteger section=[indexPath section];
	WishedItem * wishedItem= [[self wishList] objectAtIndex:row];
	[[cell textLabel]setText:[wishedItem searchKeyword]];
	[[cell detailTextLabel] setText:[wishedItem getItemStatusString]];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	NSUInteger row= [indexPath row];	
	WishedItem * wishedItem=[wishList objectAtIndex:row];
	[self navigateToDeailsView:wishedItem];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
//        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
		NSUInteger row= [indexPath row];		
		WishedItem * wishedItem= [[self wishList] objectAtIndex:row];		
		[self deleteWishedItem:wishedItem];
//		[wishedItem release];
//		[wishList removeObjectAtIndex:row];
//		[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];		
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}

@end

